package com.sgcc.pda.data.dao.impl;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;

import com.sgcc.pda.data.dao.KnowlsContentProviderDao;
import com.sgcc.pda.data.entity.knowls.DiagsInfo;
import com.sgcc.pda.data.entity.knowls.KnowInfoResult;
import com.sgcc.pda.data.entity.knowls.RepairsInfo;


import java.util.ArrayList;
import java.util.List;

/**
 * 知识库内容提供者实现.
 *
 * @author 王沛栋
 * @data 2018/3/16 16:16
 */

public class KnowlsContentProviderDaoImpl implements KnowlsContentProviderDao {
    String CONTENT_PROVIDER_CONNECT = "";

    private static KnowlsContentProviderDaoImpl contentProviderDao;

    public static KnowlsContentProviderDaoImpl getInstance() {
        if (contentProviderDao == null) {
            contentProviderDao = new KnowlsContentProviderDaoImpl();
        }
        return contentProviderDao;
    }

    public void init(String contentProviderConnect) {
        CONTENT_PROVIDER_CONNECT = contentProviderConnect;
    }

    private static final String FIND_INFO = "FIND_INFO_SUCCESS";
    private static final String FIND_INFO_BY_DEVICE_TYPE = "FIND_INFO_BY_DEVICE_TYPE";
    private static final String FIND_ALL_INFO = "FIND_ALL_INFO";
    private static final String FIND_ALL_GROUP_BY_RESONNAME = "FIND_ALL_GROUP_BY_RESONNAME";
    private static final String FIND_ALL_DIAGS_INFO = "FIND_ALL_DIAGS_INFO";
    private static final String FIND_ALL_REPAIRS_INFO = "FIND_ALL_REPAIRS_INFO";

    private static final String UPDATE_KNOW_RESULT = "UPDATE_KNOW_RESULT";
    // 更新知识库结果记录状态
    private static final String UPDATE_KNOW_INFO_RESULT = "UPDATE_KNOW_INFO_RESULT";
    // 更新知识库结果记录状态 updateKnowInfoResultName
    private static final String UPDATE_KNOW_INFO_RESULT_NAME = "UPDATE_KNOW_INFO_RESULT_NAME";
    //查询所有异常原因
    private static final String FIND_ALL_EXCEPTIONAL = "FIND_ALL_EXCEPTIONAL";
    //查询直观检查
    private static final String FIND_ZGJC_ALL = "FIND_ZGJC_ALL";


    @Override
    public List<KnowInfoResult> findInfo(Context context, String[] value, String appNo) {
        ContentResolver resolver = context.getContentResolver();
        //定义要访问的ContentProvider的
        Uri uri = Uri.parse(CONTENT_PROVIDER_CONNECT + FIND_INFO);

        Cursor cursor;
        try {
            //获取表中所有记录
            cursor = resolver.query(uri, null, null,
                    new String[]{value[0], value[1], value[2], appNo}, null);
        } catch (Exception e) {
            //  LogUtil.e("ContentProviderDaoImpl", "获取cursor报错");
            return null;
        }
        if (null == cursor) {
            return null;
        }
        List<KnowInfoResult> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            KnowInfoResult knowInfo = new KnowInfoResult();
            String rson_code = cursor.getString(cursor.getColumnIndex("RSON_CODE"));
            String e_dev_code = cursor.getString(cursor.getColumnIndex("E_DEV_CODE"));
            String rson_name = cursor.getString(cursor.getColumnIndex("RSON_NAME"));
            String app_type = cursor.getString(cursor.getColumnIndex("APP_TYPE"));
            String at_code = cursor.getString(cursor.getColumnIndex("AT_CODE"));
            String inv_type = cursor.getString(cursor.getColumnIndex("INV_TYPE"));
            String result_record = cursor.getString(cursor.getColumnIndex("RESULT_RECORD"));
            knowInfo.setAPP_NO(appNo);
            knowInfo.setE_DEV_CODE(e_dev_code);
            knowInfo.setRSON_CODE(rson_code);
            knowInfo.setRSON_NAME(rson_name);
            knowInfo.setAPP_TYPE(app_type);
            knowInfo.setAT_CODE(at_code);
            knowInfo.setINV_TYPE(inv_type);
            knowInfo.setRESULT_RECORD(result_record);
            list.add(knowInfo);

        }
        cursor.close();
        return list;
    }

    @Override
    public List<KnowInfoResult> findInfo(Context context, String abnType, String sblxbm, String inv_type) {
        ContentResolver resolver = context.getContentResolver();
        //定义要访问的ContentProvider的
        Uri uri = Uri.parse(CONTENT_PROVIDER_CONNECT + FIND_INFO_BY_DEVICE_TYPE);

        Cursor cursor;
        try {
            //获取表中所有记录
            cursor = resolver.query(uri, null, null,
                    new String[]{abnType, sblxbm, inv_type}, null);
        } catch (Exception e) {
            //  LogUtil.e("ContentProviderDaoImpl", "获取cursor报错");
            return null;
        }
        if (null == cursor) {
            return null;
        }
        List<KnowInfoResult> list = new ArrayList<>();
        while (cursor.moveToNext()) {

            KnowInfoResult knowInfo = new KnowInfoResult();
            String e_dev_code = cursor.getString(cursor.getColumnIndex("E_DEV_CODE"));

            String rson_code = cursor.getString(cursor.getColumnIndex("RSON_CODE"));
            String rson_name = cursor.getString(cursor.getColumnIndex("RSON_NAME"));
            String app_type = cursor.getString(cursor.getColumnIndex("ABN_TYPE"));
//            String at_code = cursor.getString(9);
            String at_code = cursor.getString(cursor.getColumnIndex("AT_CODE"));
            String dia_desp = cursor.getString(cursor.getColumnIndex("DIA_DESP"));

            knowInfo.setE_DEV_CODE(e_dev_code);
            knowInfo.setRSON_CODE(rson_code);
            knowInfo.setRSON_NAME(rson_name);
            knowInfo.setAT_CODE(at_code);
            knowInfo.setDIA_DESP(dia_desp);
            knowInfo.setAPP_TYPE(app_type);

            list.add(knowInfo);

        }

        cursor.close();

        return list;
    }

    @Override
    public List<KnowInfoResult> findAllInfo(Context context, String e_dev_code, String abn_type, String at_code) {
        ContentResolver resolver = context.getContentResolver();
        //定义要访问的ContentProvider的
        Uri uri = Uri.parse(CONTENT_PROVIDER_CONNECT + FIND_ALL_INFO);

        Cursor cursor;
        try {
            //获取表中所有记录
            cursor = resolver.query(uri, null, null,
                    new String[]{e_dev_code, abn_type, at_code}, null);
        } catch (Exception e) {
            //  LogUtil.e("ContentProviderDaoImpl", "获取cursor报错");
            return null;
        }
        if (null == cursor) {
            return null;
        }
        List<KnowInfoResult> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            KnowInfoResult knowInfo = new KnowInfoResult();
            String rson_code = cursor.getString(cursor.getColumnIndex("RSON_CODE"));
            String rson_name = cursor.getString(cursor.getColumnIndex("RSON_NAME"));
            knowInfo.setRSON_CODE(rson_code);
            knowInfo.setRSON_NAME(rson_name);

            list.add(knowInfo);
        }

        cursor.close();

        return list;
    }

    @Override
    public List<String> findAllGroupByResonName(Context context) {
        ContentResolver resolver = context.getContentResolver();
        //定义要访问的ContentProvider的
        Uri uri = Uri.parse(CONTENT_PROVIDER_CONNECT + FIND_ALL_GROUP_BY_RESONNAME);

        Cursor cursor;
        try {
            //获取表中所有记录
            cursor = resolver.query(uri, null, null,
                    new String[]{}, null);
        } catch (Exception e) {
            //  LogUtil.e("ContentProviderDaoImpl", "获取cursor报错");
            return null;
        }
        if (null == cursor) {
            return null;
        }
        List<String> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            String info = cursor.getString(cursor.getColumnIndex("RSON_NAME"));
            list.add(info);
        }

        cursor.close();

        return list;
    }

    @Override
    public List<DiagsInfo> findAllDiagsInfo(Context context, String code, String abnTYpe) {
        ContentResolver resolver = context.getContentResolver();
        //定义要访问的ContentProvider的
        Uri uri = Uri.parse(CONTENT_PROVIDER_CONNECT + FIND_ALL_DIAGS_INFO);

        Cursor cursor;
        try {
            if (!TextUtils.isEmpty(code) && !TextUtils.isEmpty(abnTYpe)) {
                cursor = resolver.query(uri, null, null,
                        new String[]{code, abnTYpe}, null);
            } else {
                //获取表中所有记录
                cursor = resolver.query(uri, null, null,
                        new String[]{}, null);
            }

        } catch (Exception e) {
            // LogUtil.e("ContentProviderDaoImpl", "获取cursor报错");
            return null;
        }
        if (null == cursor) {
            return null;
        }
        List<DiagsInfo> list = new ArrayList<>();
        while (cursor.moveToNext()) {

//            ABN_TYPE  RSON_CODE  DIA_CODE  STEP  RSON_NAME  DIA_TYPE  DIA_DESP  INV_TYPE  NEXT_RSON  AT_CODE
            DiagsInfo info = new DiagsInfo();
            info.setABN_TYPE(cursor.getString(cursor.getColumnIndex("ABN_TYPE")));
            info.setRSON_CODE(cursor.getString(cursor.getColumnIndex("RSON_CODE")));
            info.setDIA_CODE(cursor.getString(cursor.getColumnIndex("DIA_CODE")));
            info.setSTEP(cursor.getString(cursor.getColumnIndex("STEP")));
            info.setRSON_NAME(cursor.getString(cursor.getColumnIndex("RSON_NAME")));
            info.setDIA_TYPE(cursor.getString(cursor.getColumnIndex("DIA_TYPE")));
            info.setDIA_DESP(cursor.getString(cursor.getColumnIndex("DIA_DESP")));
            info.setINV_TYPE(cursor.getString(cursor.getColumnIndex("INV_TYPE")));
            info.setNEXT_RSON(cursor.getString(cursor.getColumnIndex("NEXT_RSON")));
            info.setAT_CODE(cursor.getString(cursor.getColumnIndex("AT_CODE")));

            list.add(info);
        }

        cursor.close();

        return list;

    }

    @Override
    public List<RepairsInfo> findAllRepairInfo(Context context, String code, String abnTYpe) {
        ContentResolver resolver = context.getContentResolver();
        //定义要访问的ContentProvider的
        Uri uri = Uri.parse(CONTENT_PROVIDER_CONNECT + FIND_ALL_REPAIRS_INFO);

        Cursor cursor;
        try {
            if (!TextUtils.isEmpty(code) && !TextUtils.isEmpty(abnTYpe)) {
                cursor = resolver.query(uri, null, null,
                        new String[]{code, abnTYpe}, null);
            } else {
                //获取表中所有记录
                cursor = resolver.query(uri, null, null,
                        new String[]{}, null);
            }

        } catch (Exception e) {
            //  LogUtil.e("ContentProviderDaoImpl", "获取cursor报错");
            return null;
        }
        if (null == cursor) {
            return null;
        }
        List<RepairsInfo> list = new ArrayList<>();
        while (cursor.moveToNext()) {

            RepairsInfo info = new RepairsInfo();
            info.setABN_TYPE(cursor.getString(cursor.getColumnIndex("ABN_TYPE")));
            info.setRSON_CODE(cursor.getString(cursor.getColumnIndex("RSON_CODE")));
            info.setREPR_CODE(cursor.getString(cursor.getColumnIndex("REPR_CODE")));
            info.setSTEP(cursor.getString(cursor.getColumnIndex("STEP")));
            info.setRSON_NAME(cursor.getString(cursor.getColumnIndex("RSON_NAME")));
            info.setREP_TYPE(cursor.getString(cursor.getColumnIndex("REP_TYPE")));
            info.setREP_DESP(cursor.getString(cursor.getColumnIndex("REP_DESP")));
            info.setINV_TYPE(cursor.getString(cursor.getColumnIndex("INV_TYPE")));
            info.setCHK_DESP(cursor.getString(cursor.getColumnIndex("CHK_DESP")));
            info.setAT_CODE(cursor.getString(cursor.getColumnIndex("AT_CODE")));

            list.add(info);
        }

        cursor.close();

        return list;
    }

    @Override
    public int updateKnowResult(Context context, String[] selections) {

        ContentResolver resolver = context.getContentResolver();
        //定义要访问的ContentProvider的
        Uri uri = Uri.parse(CONTENT_PROVIDER_CONNECT + UPDATE_KNOW_RESULT);
        ContentValues values = new ContentValues();
        values.put("testKey", "testValue");//无意义 只为不报空指针，必传
        resolver.update(uri, values, null, selections);
        return 0;
    }

    @Override
    public int updateKnowInfoResult(Context context, String[] selections) {

        ContentResolver resolver = context.getContentResolver();
        //定义要访问的ContentProvider的
        Uri uri = Uri.parse(CONTENT_PROVIDER_CONNECT + UPDATE_KNOW_INFO_RESULT);
        ContentValues values = new ContentValues();
        values.put("testKey", "testValue");//无意义 只为不报空指针，必传
        resolver.update(uri, values, null, selections);

        return 0;
    }

    @Override
    public int updateKnowInfoResultName(Context context, String[] selections) {

        ContentResolver resolver = context.getContentResolver();
        //定义要访问的ContentProvider的
        Uri uri = Uri.parse(CONTENT_PROVIDER_CONNECT + UPDATE_KNOW_INFO_RESULT_NAME);
        ContentValues values = new ContentValues();
        values.put("testKey", "testValue");//无意义 只为不报空指针，必传
        resolver.update(uri, values, null, selections);

        return 0;
    }

    @Override
    public List<KnowInfoResult> findAllExceptional(Context context, String code, String appNo) {
        Uri uri = Uri.parse(CONTENT_PROVIDER_CONNECT + FIND_ALL_EXCEPTIONAL);
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor;
        try {
            //获取表中所有记录
            cursor = resolver.query(uri, null, null,
                    new String[]{code, appNo}, null);
        } catch (Exception e) {
            //   LogUtil.e("ContentProviderDaoImpl", "获取cursor报错");
            return null;
        }
        if (null == cursor) {
            return null;
        }
        List<KnowInfoResult> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            KnowInfoResult knowInfo = new KnowInfoResult();
            String e_dev_code = cursor.getString(cursor.getColumnIndex("E_DEV_CODE"));

            String rson_code = cursor.getString(cursor.getColumnIndex("RSON_CODE"));
            String rson_name = cursor.getString(cursor.getColumnIndex("RSON_NAME"));
            String app_type = cursor.getString(cursor.getColumnIndex("APP_TYPE"));
//            String at_code = cursor.getString(9);
            String at_code = cursor.getString(cursor.getColumnIndex("AT_CODE"));
            String dia_desp = cursor.getString(cursor.getColumnIndex("DIA_DESP"));

            knowInfo.setE_DEV_CODE(e_dev_code);
            knowInfo.setRSON_CODE(rson_code);
            knowInfo.setRSON_NAME(rson_name);
            knowInfo.setAT_CODE(at_code);
            knowInfo.setAPP_TYPE(app_type);
            knowInfo.setDIA_DESP(dia_desp);

            list.add(knowInfo);

        }

        cursor.close();

        return list;
    }

    @Override
    public List<KnowInfoResult> findZGJCAll(Context context, String app_no, String inv_type) {
        Uri uri = Uri.parse(CONTENT_PROVIDER_CONNECT + FIND_ZGJC_ALL);
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor;
        try {
            //获取表中所有记录
            cursor = resolver.query(uri, null, null,
                    new String[]{app_no, inv_type}, null);
        } catch (Exception e) {
            //    LogUtil.e("ContentProviderDaoImpl", "获取cursor报错");
            return null;
        }
        if (null == cursor) {
            return null;
        }
        List<KnowInfoResult> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            KnowInfoResult knowInfo = new KnowInfoResult();
            String e_dev_code = cursor.getString(cursor.getColumnIndex("E_DEV_CODE"));

            String rson_code = cursor.getString(cursor.getColumnIndex("RSON_CODE"));
            String rson_name = cursor.getString(cursor.getColumnIndex("RSON_NAME"));
            String app_type = cursor.getString(cursor.getColumnIndex("ABN_TYPE"));
//            String at_code = cursor.getString(9);
            String at_code = cursor.getString(cursor.getColumnIndex("AT_CODE"));
            String dia_desp = cursor.getString(cursor.getColumnIndex("DIA_DESP"));

            knowInfo.setE_DEV_CODE(e_dev_code);
            knowInfo.setRSON_CODE(rson_code);
            knowInfo.setRSON_NAME(rson_name);
            knowInfo.setAT_CODE(at_code);
            knowInfo.setAPP_TYPE(app_type);
            knowInfo.setDIA_DESP(dia_desp);

            list.add(knowInfo);

        }

        cursor.close();

        return list;
    }
}
